//
//  HNTFHViewController.m
//  Estate
//
//  Created by heyong on 16/6/13.
//  Copyright © 2016年 ttsx. All rights reserved.
//

#import "HNTFHViewController.h"
#import "B_SwitchCell.h"
#import "B_wuLiaoCell.h"
@interface HNTFHViewController ()
@end

@implementation HNTFHViewController
-(NSMutableArray *)selectedAssetURLs{
    if(_selectedAssetURLs==nil){
        _selectedAssetURLs=[NSMutableArray array];
    }
    return  _selectedAssetURLs;
}
- (Site_04 *)site{
    if(_site==nil){
        _site=[[Site_04 alloc]init];
    }
    return _site;
}

- (NSMutableDictionary *)dataDic{
    if(_dataDic==nil){
        _dataDic=[NSMutableDictionary dictionary];
    }
    return _dataDic;
}

-(TPKeyboardAvoidingTableView *)tableView{
    if(_tableView==nil){
        _tableView=[[TPKeyboardAvoidingTableView alloc]initWithFrame:CGRECTS(0, 0, 375, 667)];
        _tableView.showsVerticalScrollIndicator=NO;
        _tableView.delegate=self;
        _tableView.dataSource=self;
        _tableView.bounces=NO;
        
        [LSUtility setExtraCellLineHidde:_tableView tabHeader:NO tabFooter:NO];
        
        [_tableView registerClass:[H1_ImagesCell class] forCellReuseIdentifier:kCellIdentifier_H1_ImagesCell];
        [_tableView registerNib:[UINib nibWithNibName:@"B_wuLiaoCell" bundle:nil] forCellReuseIdentifier:@"wuliao"];
        [_tableView registerNib:[UINib nibWithNibName:@"B_SwitchCell" bundle:nil] forCellReuseIdentifier:@"switch"];
    }
    return _tableView;
}

- (UIImagePickerController *)picker{
    if(!_picker){
        _picker = [[UIImagePickerController alloc] init];
        _picker.delegate = self;
        _picker.allowsEditing = NO;//设置可编辑
        _picker.sourceType = UIImagePickerControllerSourceTypeCamera;
    }
    return _picker;
}

- (void)viewDidLoad {
    [super viewDidLoad];
      self.view.backgroundColor=[UIColor colorWithRed:0.98 green:0.98 blue:0.98 alpha:1.00];
    [self.view addSubview: self.tableView];
    _curTweet = [[Tweet alloc] init];

    [self setData];
    NSNotificationCenter * center = [NSNotificationCenter defaultCenter];
    [center addObserver:self selector:@selector(refresh:) name:@"RefreshImage_4" object:nil];
}

- (void)refresh:(id)sender{
    if(self.selectedAssetURLs!=nil){
        dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{
            //设置图片
            self.curTweet.selectedAssetURLs =self.selectedAssetURLs;
            NSLog(@"self.curTweet.selectedAssetURLs:%@",self.curTweet.selectedAssetURLs);
            //回到主线程来刷新
            dispatch_async(dispatch_get_main_queue(), ^{
                [self.tableView reloadRowsAtIndexPaths:[NSArray arrayWithObject:[NSIndexPath indexPathForRow:0 inSection:3]] withRowAnimation:UITableViewRowAnimationFade];
            });
        });
    }
}


-(void)setData{
    WEAKSELF
    if(_site!=nil){
        
        [weakSelf.dataDic setObject:_site.fh_1 forKey:hnt_fh_1];
        [weakSelf.dataDic setObject:_site.fh_2 forKey:hnt_fh_2];
        [weakSelf.dataDic setObject:_site.fh_3 forKey:hnt_fh_3];
        [weakSelf.dataDic setObject:_site.fh_4 forKey:hnt_fh_4];
        [weakSelf.dataDic setObject:_site.fh_5 forKey:hnt_fh_5];
        [weakSelf.dataDic setObject:_site.fh_6 forKey:hnt_fh_6];
        [weakSelf.dataDic setObject:_site.fh_7 forKey:hnt_fh_7];
        [weakSelf.dataDic setObject:_site.fh_8 forKey:hnt_fh_8];
        [weakSelf.dataDic setObject:_site.fh_9 forKey:hnt_fh_9];
        [weakSelf.dataDic setObject:_site.fh_10 forKey:hnt_fh_10];
        self.selectedAssetURLs= [LSUtility quertFileImageType:@"RECORD_02_SIDE_04" andId:_site.recordId andFilePathArr:_site.fileArray];
        [self refresh:nil];
        
    }else{
        //设置默认值
        [weakSelf.dataDic setObject:@"是" forKey:hnt_fh_1];
        [weakSelf.dataDic setObject:@"是" forKey:hnt_fh_2];
        [weakSelf.dataDic setObject:@"是" forKey:hnt_fh_3];
        [weakSelf.dataDic setObject:@"是" forKey:hnt_fh_4];
        [weakSelf.dataDic setObject:@"是" forKey:hnt_fh_5];
        [weakSelf.dataDic setObject:@"是" forKey:hnt_fh_6];
        [weakSelf.dataDic setObject:@"是" forKey:hnt_fh_7];
        [weakSelf.dataDic setObject:@"是" forKey:hnt_fh_8];
        [weakSelf.dataDic setObject:@"是" forKey:hnt_fh_9];
        [weakSelf.dataDic setObject:@"是" forKey:hnt_fh_10];
        
    }
    
}

#pragma mark-UITaleiViewDelegate
-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    if(section==0){
        return 8;
    }else if (section==1){
        return 2;
    }else{
        return 1;
    }
}
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
    if(indexPath.section==2){
        return 120;
    }else if(indexPath.section==3){
        return  [H1_ImagesCell cellHeightWithObj:_curTweet title:@"附件"];
    }else{
        return 44;
    }
}

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView{
    return 4;
}

- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section{
    if(section==0){
        return 0;
    }else
        return 10.;
}

-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    WEAKSELF
    long section=indexPath.section;
    long row=indexPath.row;
    if(section==0){
        B_SwitchCell *cell=[tableView dequeueReusableCellWithIdentifier:@"switch" forIndexPath:indexPath];
        if(row==0){
            cell.title.text=hnt_fh_1;
            cell.isEdit=self.isEdit;
            cell.sValueChangeBlock=^(UISwitch *sender){
                weakSelf.site.fh_1=sender.on==0?@"否":@"是";
                [weakSelf.dataDic setObject:weakSelf.site.fh_1 forKey:hnt_fh_1];
            };
            
            if(_site){
                if([_site.fh_1 isEqualToString:@"是"]){
                    //设置开关
                    [cell.kaiguan setOn:YES];
                }else{
                    [cell.kaiguan setOn:NO];
                }
            }
        }else if (row==1){
            cell.title.text=hnt_fh_2;
            cell.isEdit=self.isEdit;
            cell.sValueChangeBlock=^(UISwitch *sender){
                weakSelf.site.fh_2=sender.on==0?@"否":@"是";
                [weakSelf.dataDic setObject:weakSelf.site.fh_2 forKey:hnt_fh_2];
            };
            
            if(_site){
                if([_site.fh_2 isEqualToString:@"是"]){
                    //设置开关
                    [cell.kaiguan setOn:YES];
                }else{
                    [cell.kaiguan setOn:NO];
                }
            }
        
        }else if (row==2){
            cell.title.text=hnt_fh_3;
            cell.isEdit=self.isEdit;
            cell.sValueChangeBlock=^(UISwitch *sender){
                weakSelf.site.fh_3=sender.on==0?@"否":@"是";
                [weakSelf.dataDic setObject:weakSelf.site.fh_3 forKey:hnt_fh_3];
            };
            
            if(_site){
                if([_site.fh_3 isEqualToString:@"是"]){
                    //设置开关
                    [cell.kaiguan setOn:YES];
                }else{
                    [cell.kaiguan setOn:NO];
                }
            }
        
        }else if (row==3){
            cell.title.text=hnt_fh_1;
            cell.isEdit=self.isEdit;
            cell.sValueChangeBlock=^(UISwitch *sender){
                weakSelf.site.fh_4=sender.on==0?@"否":@"是";
                [weakSelf.dataDic setObject:weakSelf.site.fh_4 forKey:hnt_fh_4];
            };
            
            if(_site){
                if([_site.fh_4 isEqualToString:@"是"]){
                    //设置开关
                    [cell.kaiguan setOn:YES];
                }else{
                    [cell.kaiguan setOn:NO];
                }
            }
        
        }else if (row==4){
            cell.title.text=hnt_fh_5;
            cell.isEdit=self.isEdit;
            cell.sValueChangeBlock=^(UISwitch *sender){
                weakSelf.site.fh_5=sender.on==0?@"否":@"是";
                [weakSelf.dataDic setObject:weakSelf.site.fh_5 forKey:hnt_fh_5];
            };
            
            if(_site){
                if([_site.fh_5 isEqualToString:@"是"]){
                    //设置开关
                    [cell.kaiguan setOn:YES];
                }else{
                    [cell.kaiguan setOn:NO];
                }
            }
        
        }else if (row==5){
            cell.title.text=hnt_fh_6;
            cell.isEdit=self.isEdit;
            cell.sValueChangeBlock=^(UISwitch *sender){
                weakSelf.site.fh_6=sender.on==0?@"否":@"是";
                [weakSelf.dataDic setObject:weakSelf.site.fh_6 forKey:hnt_fh_6];
            };
            
            if(_site){
                if([_site.fh_6 isEqualToString:@"是"]){
                    //设置开关
                    [cell.kaiguan setOn:YES];
                }else{
                    [cell.kaiguan setOn:NO];
                }
            }
        
        }else if (row==6){
        
            cell.title.text=hnt_fh_7;
            cell.isEdit=self.isEdit;
            cell.sValueChangeBlock=^(UISwitch *sender){
                weakSelf.site.fh_7=sender.on==0?@"否":@"是";
                [weakSelf.dataDic setObject:weakSelf.site.fh_7 forKey:hnt_fh_7];
            };
            
            if(_site){
                if([_site.fh_7 isEqualToString:@"是"]){
                    //设置开关
                    [cell.kaiguan setOn:YES];
                }else{
                    [cell.kaiguan setOn:NO];
                }
            }
        }else {
            cell.title.text=hnt_fh_8;
            cell.isEdit=self.isEdit;
            cell.sValueChangeBlock=^(UISwitch *sender){
                weakSelf.site.fh_8=sender.on==0?@"否":@"是";
                [weakSelf.dataDic setObject:weakSelf.site.fh_8 forKey:hnt_fh_8];
            };
            if(_site){
                
                if([_site.fh_8 isEqualToString:@"是"]){
                    //设置开关
                    [cell.kaiguan setOn:YES];
                }else{
                    [cell.kaiguan setOn:NO];
                }
            }
        }
        
        return cell;
    }else if(section==1){
        B_SwitchCell *cell=[tableView dequeueReusableCellWithIdentifier:@"switch" forIndexPath:indexPath];
        cell.selectionStyle=UITableViewCellSelectionStyleNone;
        if(row==0){
            cell.title.text=hnt_fh_9;
            cell.isEdit=self.isEdit;
            cell.sValueChangeBlock=^(UISwitch *sender){
                weakSelf.site.fh_9=sender.on==0?@"否":@"是";
                [weakSelf.dataDic setObject:weakSelf.site.fh_9 forKey:hnt_fh_9];
            };
            if(_site){
                if([_site.fh_9 isEqualToString:@"是"]){
                    //设置开关
                    [cell.kaiguan setOn:YES];
                }else{
                    [cell.kaiguan setOn:NO];
             
                }
            }
            
        }else{
            cell.title.text=hnt_fh_10;
            cell.isEdit=self.isEdit;
            cell.sValueChangeBlock=^(UISwitch *sender){
                weakSelf.site.fh_10=sender.on==0?@"否":@"是";
                [weakSelf.dataDic setObject:weakSelf.site.fh_10 forKey:hnt_fh_10];
            };
            
            if(_site){
                if([_site.fh_10 isEqualToString:@"是"]){
                    //设置开关
                    [cell.kaiguan setOn:YES];
                }else{
                    [cell.kaiguan setOn:NO];
                }
            }
        }
        return cell;
    }else if (section==3){
        WEAKSELF
        H1_ImagesCell *cell = [tableView dequeueReusableCellWithIdentifier:kCellIdentifier_H1_ImagesCell forIndexPath:indexPath];
        [cell setTitle:@"附件"];
        cell.isEdit=self.isEdit;
        cell.curTweet = _curTweet;
        cell.addPicturesBlock = ^(){
            [weakSelf showActionForPhoto];
        };
        __block H1_ImagesCell *cellblock = cell;
        cell.deleteTweetImageBlockUrl = ^(TweetImage *toDelete,NSString *deurl){
            [cellblock.curTweet.tweetImages removeObject:toDelete];
            [weakSelf.selectedAssetURLs removeObject:deurl];
            [weakSelf refresh:nil];
        };
        _tableView.separatorStyle = UITableViewCellSeparatorStyleNone;
        return cell;
    
    } else{
        B_wuLiaoCell *cell=[tableView dequeueReusableCellWithIdentifier:@"wuliao" forIndexPath:indexPath];
        cell.title.text=fs_msg_10;
        cell.isEdit=self.isEdit;
        cell.textValueChangedBlock=^(NSString *valuerStr){
            weakSelf.site.text=@{@"内容":valuerStr};
            [weakSelf.dataDic setObject:weakSelf.site.text forKey:fs_msg_10];
        };
        if(_site){
            cell.text.text=_site.text[@"内容"];
        }
        
        
        return  cell;
    }
}
#pragma mark UIActionSheet M

- (void)showActionForPhoto{
    //    @weakify(self);
    [[UIActionSheet bk_actionSheetCustomWithTitle:nil buttonTitles:@[@"拍照", @"从相册选择"] destructiveTitle:nil cancelTitle:@"取消" andDidDismissBlock:^(UIActionSheet *sheet, NSInteger index) {
        
        [self photoActionSheet:sheet DismissWithButtonIndex:index];
        
    }] showInView:self.view];
}

- (void)photoActionSheet:(UIActionSheet *)sheet DismissWithButtonIndex:(NSInteger)buttonIndex{
    if (buttonIndex == 0) {
        //拍照
        if (_curTweet.tweetImages.count >= 9) {//照片最多这么多
            kTipAlert(@"最多只可选择9张照片，已经选满了。先去掉一张照片再拍照呗～");
            return;
        }
             [self presentViewController:self.picker animated:YES completion:nil];//进入照相界面
    }else if (buttonIndex == 1){
        //相册
        PictureViewController *picCtr=[[PictureViewController alloc]init];
        picCtr.flag=@"file";
        NSMutableArray *array = [self.selectedAssetURLs valueForKeyPath:@"@distinctUnionOfObjects.self"];
        picCtr.selectedImages=[[NSMutableArray alloc] initWithArray:array];
        picCtr.selectedImageBlock=^(NSMutableArray *array){
            for (NSString *path in array) {
                //添加图片路径到已选数组
                [self.selectedAssetURLs addObject:path];
            }
        };
        [self.navigationController pushViewController:picCtr animated:YES];
    }
}

#pragma mark UIImagePickerControllerDelegate

- (void)imagePickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo:(NSDictionary *)info{
    UIImage *image=[info objectForKey:UIImagePickerControllerOriginalImage];
    self.curTweet.editImageView = [[EditImageView alloc]initWithImage:image];
    WEAKSELF
    self.curTweet.editImageView.completeBlock = ^(NSString *path) {
        //添加选择附件
        [weakSelf.curTweet addASelectedAssetURL:path];
        [weakSelf.selectedAssetURLs addObject:path];
        //刷新指定行
        [weakSelf.tableView reloadRowsAtIndexPaths:[NSArray arrayWithObject:[NSIndexPath indexPathForRow:0 inSection:3]] withRowAnimation:UITableViewRowAnimationFade];
    };
    [picker dismissViewControllerAnimated:YES completion:nil];
}

- (void)imagePickerControllerDidCancel:(UIImagePickerController *)picker{
    [picker dismissViewControllerAnimated:YES completion:nil];
}
#pragma mark scrollview
- (void)scrollViewWillBeginDragging:(UIScrollView *)scrollView{
    if (scrollView == self.tableView) {
        [self.view endEditing:YES];
    }
}

-(void)dealloc{
    [[NSNotificationCenter defaultCenter] removeObserver:self];
}
@end
